package com.starbaba.system.userofrole.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.Action;
import com.starbaba.controllog.aop.LogAble;
import com.starbaba.controllog.model.ControlLogCategory;
import com.starbaba.core.hibernate.Page;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.web.sequence.Sequence;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.system.index.AuthorityAction;
import com.starbaba.system.userofrole.model.ExtUserOfRoleVO;
import com.starbaba.system.userofrole.model.UserOfRoleVO;

@SuppressWarnings({"serial","rawtypes"})
public class UserofroleAction extends BaseAction {
	private ArrayList<ControlLogCategory> logCategorysArrayList;
	public UserofroleAction() {
		this.voClass = UserOfRoleVO.class;
		this.pkNameArray = new String[] { "id" };
		this.setSaveLog(true);
		logCategorysArrayList = new ArrayList<ControlLogCategory>();
	}
	
	@Override
	protected void beforeAdd() throws Exception {
		UserOfRoleVO vo = new UserOfRoleVO();
		vo.setId(Sequence.getSequence());
		this.setEntityVO(vo);
	}
	
	public String showPage(){
		return "success";
	}

	
	@Override
	public String list() {
		
		try{
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
			this.setParamsMapToTargetObject(paramsMap, this.getEntityListVO());
			
			String _seq_userid = (String)paramsMap.get("_seq_userid");
			String _leq_roleid = (String)paramsMap.get("_leq_roleid");
			
			StringBuffer sql = new StringBuffer();
			sql.append("select ur.* from sys_user_role ur, sys_user u where u.userid = ur.userid and u.comid = ? ");
			
			List<Object> params = new ArrayList<Object>();
			params.add(userVO.getComid());//公司权限
			
			if(StringUtils.isNotBlank(_seq_userid)){
				sql.append(" and ur.userid = ? ");
				params.add(_seq_userid);
			}
			if(StringUtils.isNotBlank(_leq_roleid)){
				sql.append(" and ur.roleid = ? ");
				params.add(_leq_roleid);
			}
			
			page = (Page) ObjectUtils.defaultIfNull(page, new Page());
			BeanUtils.copyProperties(page, this.getEntityListVO());
			page = this.getService().doQuery(page, sql.toString(),ExtUserOfRoleVO.class, params.toArray());
		}catch(Exception ex){
			log.error(ex, ex);
			this.addErrorMessage(ex.getMessage());
			return Action.ERROR;
		}
		return Action.SUCCESS;
	}

	@Override
	protected void beforeSave() throws Exception {
		String sql = "from UserOfRoleVO where userid = ? and roleid = ? ";
		UserOfRoleVO vo = (UserOfRoleVO) this.getEntityVO();
		ArrayList list = (ArrayList) this.getService().doFindByHQL(sql,
				new Object[] { vo.getUserid(), vo.getRoleid() });
		if (null != list && list.size() > 0) {
			throw new Exception("用户角色关系已经存在");
		}

	}

	@Override
	protected void beforeUpdate() throws Exception {
		String sql = "from UserOfRoleVO where userid = ? and roleid = ? and id != ?";
		UserOfRoleVO vo = (UserOfRoleVO) this.getEntityVO();
		ArrayList list = (ArrayList) this.getService().doFindByHQL(sql,new Object[] { vo.getUserid(), vo.getRoleid() ,vo.getId()});
		UserOfRoleVO oldVO = (UserOfRoleVO) this.getService().doFindByPk(
				voClass, vo.getId());

		if (null != list && list.size() > 0) {
			if (!list.get(0).equals(oldVO))
				throw new Exception("用户角色关系已经存在");
		}
	}
	
	//获取菜单
	public void getList() {
		String listStr = "";
		List groupids = null;
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		//type为0,获取权限菜单,为1获取全部菜单
		String type = (String) paramsMap.get("type");
		String groupidsStr1 = (String) paramsMap.get("groupid");
		if(groupidsStr1 != null){
			String[] groupidsStr2= groupidsStr1.split(",");
			groupids = Arrays.asList(groupidsStr2);
		}
		try{
			AuthorityAction list = new AuthorityAction();
			if(Integer.valueOf(type) == 0){
				listStr = list.executeGetList(0, groupids, "1");
			}else{
				listStr = list.executeGetList(1, groupids, "1");
			}
			sendJsonMessage(listStr);
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	//修改菜单
	@LogAble (operate=LogAble.OPERATE_MODIFY)
	public void changeAuth() {
		List menuList = null;
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(
				Struts2Utils.getRequest(), paramsMap);
		//type为1时,新增权限,为0时删除权限
		String type = (String) paramsMap.get("type");
		String groupid = (String) paramsMap.get("groupid");
		String creator = (String) paramsMap.get("creator");
		String menuList1 = (String) paramsMap.get("menuList");
		if(groupid == null){
			return;
		}
		if(menuList1 != null){
			String[] menuList2= menuList1.split("#");
			menuList = Arrays.asList(menuList2);
		}
		
		try{
			AuthorityAction list = new AuthorityAction();
			String respString = list.changeAuth(menuList, groupid, type, creator);
			sendJsonMessage(respString);
			
			ControlLogCategory logCategory = new ControlLogCategory();
			logCategory.setId(groupid);
			logCategory.setName("1新增权限,0删除权限,当前为" + type);
			logCategorysArrayList.add(logCategory);
			
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	@Override
	public String getActionId() {
		return "1000000802";
	}
	
	@Override
	public ArrayList<ControlLogCategory> getProduct() {
		return logCategorysArrayList;
	}
	
	//修改组
	

}
